#pragma once
#include "defines.h"
#include "netinterface.h"
#include "critical_lock.h"
#include "loki/SmallObj.h"
#include <set>

class CMessageQueue	:	public IMessageQueue
{
public:
	CMessageQueue(void);
	~CMessageQueue(void);

private:
	virtual INetPacket* PickMessage();
	virtual INetPacket* PopMessage();
	virtual void PushMessage( INetPacket* );
	virtual void Release();

private:
	struct _node 
#if defined(_DEBUG) && defined(_USE_MEM_POOL)
		: public Loki::SmallObject<>
#endif
	{
		INetPacket* pPacket;
		volatile long		index;
		volatile _node*		next;
	};
	
	volatile _node *head, *tail, *free_head, *free_tail;

	volatile 
	_node*	alloc_node();
	void	free_node( volatile _node* p );

	volatile 
	long	m_count, m_shutdown, m_index;

	typedef std::set< size_t >	_handle_set;
	_handle_set m_handle_set;

	CCritical	m_msg_guard;
	CCritical	m_free_guard;
};
